import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

// 让vue框架应用vuex
Vue.use(Vuex)

export default ({
    state: {
        hotList: [],
        hotTotal: 0, //热门房源总数
        pn: 1 //页码初始值1
    },
    mutations: {
        //在mutations中设置state块中的属性值（通过设置方法）
        setHotList(state, value) {
            state.hotList = state.hotList.concat(value)
            // console.log(value)
        }
    },
    actions: {
        getHotList({
            commit,
            state
        }, pn) {
            let start = (pn - 1) * 3;
            let end = start + 3;

            axios({
                method: 'get',
                url: '/data/news_data.json'
            }).then((res) => {
                let data = res.data
                // console.log(data)
                if (data.code === 200) {

                    commit('setHotList', data.hotList.slice(start, end)) //调用mutations中的setHotList()方法给state中的hotList属性设置值
                    state.hotTotal = data.hotList.length
                }
                // state.hotList = data.hotList
                // console.log("这是state中的hotList：", state.hotList)
            }).catch((err) => {
                console.log(err)
            })
        }
    }
})